package com.xdqzy.baseline.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

//    @Bean("redisTemplate")
//    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisFactory) {
//        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisFactory);
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        redisTemplate.setKeySerializer(stringRedisSerializer);
//        redisTemplate.setHashKeySerializer(stringRedisSerializer);
//        redisTemplate.setValueSerializer(stringRedisSerializer);
//        redisTemplate.setHashValueSerializer(stringRedisSerializer);
//        redisTemplate.setDefaultSerializer(stringRedisSerializer);
//        return redisTemplate;
//    }

    @Bean("redisTemplate")
    @Primary
    public RedisTemplate<String, Object> commonRedisTemplate(RedisConnectionFactory redisFactory) {
        RedisTemplate<String, Object> commonRedisTemplate = new RedisTemplate<>();
        commonRedisTemplate.setConnectionFactory(redisFactory);
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        commonRedisTemplate.setKeySerializer(stringRedisSerializer);
        commonRedisTemplate.setValueSerializer(jsonRedisSerializer);
        commonRedisTemplate.setHashKeySerializer(stringRedisSerializer);
        commonRedisTemplate.setDefaultSerializer(jsonRedisSerializer);
        return commonRedisTemplate;
    }
}
